﻿using MiniFox.Components;
using MiniFox.Data.Configurations;
using System.Data;

namespace MiniFox.Data
{
    /// <summary>
    /// 
    /// </summary>
    [AutoRegister, SpareComponent(typeof(DataSourceFactory))]
    public class DataSourceFactory
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected virtual Database CreateDatabase(string connectionName)
        {
            return Database.CreateDatabase(connectionName);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cmdText"></param>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        protected IDataCommand CreateDataCommand(string name, string cmdText, string connectionName)
        {
            return CreateDataCommand(name, cmdText, CommandType.Text, connectionName);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        protected virtual IDataCommand CreateDataCommand(string name, string cmdText, CommandType cmdType, string connectionName)
        {
            var database = this.CreateDatabase(connectionName);

            return CreateDataCommand(name, cmdText, cmdType, database);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <param name="database"></param>
        /// <returns></returns>
        protected IDataCommand CreateDataCommand(string name, string cmdText, CommandType cmdType, Database database)
        {
            return database.CreateDataCommand(name, cmdText, cmdType);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public virtual IDataCommand GetDataCommand(string name)
        {
            CommandConfiguration commandConfig = Database.Configuration.Commands[name];
            IDataCommand dataCommand = CreateDataCommand(commandConfig.Name, commandConfig.Text, commandConfig.CommandType, commandConfig.ConnectionName);
            dataCommand.TimeOut = commandConfig.TimeOut;
            dataCommand.Preparable = commandConfig.Prepare;
            if (commandConfig.ParameterSettings != null)
            foreach (ParameterSetting par in commandConfig.ParameterSettings)
            {
                Parameter p = new Parameter(par.Name) { ParameterName = par.ParameterName, ClrType = par.ClrType, Direction = par.Direction, Nullable = par.Nullable, DefaultValue = par.DefaultValue };
                p.Precision = par.Precision;
                p.Scale = par.Scale;
                p.Size = par.Size;
                dataCommand.Parameters.Add(p);
            }
            return dataCommand;
        }
    }
}
